package com.diyunapp.xilinapp.myorders;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.selfdefine.FragmentAdapter;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by NIDAYE on 2016/6/21.
 */
public class MyOrdersActivity extends StatusBarFragmentActivity implements ViewPager.OnPageChangeListener {

    @Bind(R.id.myorders_goback_ib)
    ImageButton myordersGobackIb;
    @Bind(R.id.orders_total_rb)
    RadioButton ordersTotalRb;
    @Bind(R.id.orders_forpay_rb)
    RadioButton ordersForpayRb;
    @Bind(R.id.orders_forsend_rb)
    RadioButton ordersForsendRb;
    @Bind(R.id.orders_forreceive_rb)
    RadioButton ordersForreceiveRb;
    @Bind(R.id.orders_forevaluation_rb)
    RadioButton ordersForevaluationRb;
    @Bind(R.id.my_orders_vp)
    ViewPager myOrdersVp;
    @Bind(R.id.myorders_select_rg)
    RadioGroup myordersSelectRg;


    private View headView;

    private List<Fragment> myOrdersList;
    private FragmentAdapter myOrdersPagerAp;

    private Fragment totalOrderFrag;
    private Fragment forPayOrderFrag;
    private Fragment forSendOrderFrag;
    private Fragment forReceiveOrderFrag;
    private Fragment forEvaluationOrderFrag;

    private int selectOrders = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_orders_layout);
        ButterKnife.bind(this);

        selectOrders = getIntent().getIntExtra("OrderState",0);

        headView = findViewById(R.id.myorders_menubar_inc);
        super.MenuWidget(headView);

        initViews();
        clickRadioButton(selectOrders);
    }

    private void initViews() {

        myordersGobackIb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        myOrdersList = new ArrayList<>();
        totalOrderFrag = new TotalOrdersFragment();
        forPayOrderFrag = new ForPayOrdersFragment();
        forSendOrderFrag = new ForSendOrdersFragment();
        forReceiveOrderFrag = new ForReceiveOrdersFragment();
        forEvaluationOrderFrag = new ForEvaluationOrdersFragment();

        myOrdersList.add(totalOrderFrag);
        myOrdersList.add(forPayOrderFrag);
        myOrdersList.add(forSendOrderFrag);
        myOrdersList.add(forReceiveOrderFrag);
        myOrdersList.add(forEvaluationOrderFrag);

        myOrdersPagerAp = new FragmentAdapter(this.getSupportFragmentManager(), myOrdersList);
        myOrdersVp.setAdapter(myOrdersPagerAp);
        myOrdersVp.setOnPageChangeListener(this);
        myOrdersPagerAp.notifyDataSetChanged();

        myordersSelectRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId)
                {
                    case R.id.orders_total_rb:
                        myOrdersVp.setCurrentItem(0);
                        break;
                    case R.id.orders_forpay_rb:
                        myOrdersVp.setCurrentItem(1);
                        break;
                    case R.id.orders_forsend_rb:
                        myOrdersVp.setCurrentItem(2);
                        break;
                    case R.id.orders_forreceive_rb:
                        myOrdersVp.setCurrentItem(3);
                        break;
                    case R.id.orders_forevaluation_rb:
                        myOrdersVp.setCurrentItem(4);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    private void clickRadioButton(int state)
    {
        switch (state) {
            case 0:
                ordersTotalRb.setChecked(true);
                break;
            case 1:
                ordersForpayRb.setChecked(true);
                break;
            case 2:
                ordersForsendRb.setChecked(true);
                break;
            case 3:
                ordersForreceiveRb.setChecked(true);
                break;
            case 4:
                ordersForevaluationRb.setChecked(true);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Log.i("zhangxiaoxidebug", "evaluation position=" + position);
        clickRadioButton(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
